package com.isdepci.project.carbon.service.impl;

import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.carbon.domain.BFossilFuelParameter;
import com.isdepci.project.carbon.domain.BSolidFuel;
import com.isdepci.project.carbon.domain.param.WeatherDataExcel;
import com.isdepci.project.carbon.mapper.BFossilFuelParameterMapper;
import com.isdepci.project.carbon.mapper.BSolidFuelMapper;
import com.isdepci.project.carbon.service.IBSolidFuelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 固体燃料燃烧碳排放Service业务层处理
 *
 * @author isdepci
 * @date 2025-03-29
 */
@Service
public class BSolidFuelServiceImpl implements IBSolidFuelService
{
    @Autowired
    private BSolidFuelMapper bSolidFuelMapper;
    @Autowired
    private BFossilFuelParameterMapper fuelParameterMapper;



    public BFossilFuelParameter getFossilFuelParameters(String fuelName) {
        // 根据燃料名称查询 b_fossil_fuel_parameter 表中的参数
        return fuelParameterMapper.findByFuelName(fuelName);
    }

    @Override
    public AjaxResult calSolidFuel(Long projectId) {
        // 查询所有BSolidFuel数据
        List<BSolidFuel> bSolidFuels = bSolidFuelMapper.selectBSolidFuelByProjectId(projectId);
        // 初始化总的碳排放量
        BigDecimal totalEmissions = BigDecimal.ZERO;
        for (BSolidFuel bSolidFuel : bSolidFuels) {
            // 计算EF
            BigDecimal EF = (bSolidFuel.getHeatCarbonContent()
                    .multiply(bSolidFuel.getCarbonOxidationRate().divide(new BigDecimal("100")))  // 转换为小数
                    .multiply(new BigDecimal("44"))
                    .divide(new BigDecimal("12"), 6, BigDecimal.ROUND_HALF_UP));
            // 计算AD
            BigDecimal AD = bSolidFuel.getLowerHeatValue()
                    .multiply(bSolidFuel.getPurchaseQuantity().subtract(bSolidFuel.getInventoryLevel()));
            // 计算碳排放量 = EF * AD
            BigDecimal emissions = EF.multiply(AD);
            bSolidFuel.setCarbonEmissions(emissions);
            bSolidFuelMapper.updateBSolidFuel(bSolidFuel);
            // 累加碳排放量
            totalEmissions = totalEmissions.add(emissions);
        }
        return AjaxResult.success(totalEmissions.setScale(2, RoundingMode.HALF_UP));
    }

    public AjaxResult changeDataByProjectId(WeatherDataExcel excel) {
        List<BSolidFuel> list =  excel.getSolidFuelList();
        list.forEach(item->{
            if(excel.getProjectId()!=null){
                item.setProjectId(excel.getProjectId());
            }
        });
        if(list.size()>0){
            bSolidFuelMapper.insertList(list);
        }
        return AjaxResult.success();
    }


    /**
     * 查询固体燃料燃烧碳排放
     */
    @Override
    public BSolidFuel selectBSolidFuelById(Long id)
    {
        return bSolidFuelMapper.selectBSolidFuelById(id);
    }

    /**
     * 查询固体燃料燃烧碳排放列表
     */
    @Override
    public List<BSolidFuel> selectBSolidFuelList(BSolidFuel bSolidFuel)
    {
        return bSolidFuelMapper.selectBSolidFuelList(bSolidFuel);
    }

    /**
     * 新增固体燃料燃烧碳排放
     */
    @Override
    public int insertBSolidFuel(BSolidFuel bSolidFuel)
    {
        return bSolidFuelMapper.insertBSolidFuel(bSolidFuel);
    }

    /**
     * 修改固体燃料燃烧碳排放
     */
    @Override
    public int updateBSolidFuel(BSolidFuel bSolidFuel)
    {
        return bSolidFuelMapper.updateBSolidFuel(bSolidFuel);
    }

    /**
     * 批量删除固体燃料燃烧碳排放
     */
    @Override
    public int deleteBSolidFuelByIds(Long[] ids)
    {
        return bSolidFuelMapper.deleteBSolidFuelByIds(ids);
    }

    /**
     * 删除固体燃料燃烧碳排放信息
     */
    @Override
    public int deleteBSolidFuelById(Long id)
    {
        return bSolidFuelMapper.deleteBSolidFuelById(id);
    }


}
